package tests;
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 10715
 * Date: 2024-08-27
 * Time: 20:33
 */
public class UpdatePage extends Utils {
    public static String url = "http://192.168.47.135:8653/blog_update.html?id=30";
    Class<?> targetclass = UpdatePage.class;

    public UpdatePage() {
        super(url);
    }

    public void updateByMySuccess() {
        String methodName="";
        driver.navigate().refresh();
        WebElement element = driver.findElement(By.cssSelector("#title"));
        String title1 = element.getText();
        assert !title1.isEmpty();
        try {
            methodName=targetclass.getMethod("updateByMySuccess").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        element.clear();
        System.out.println("updateByMySuccess---" + title1);
        element.sendKeys("今天good");
        //提交
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait webDriverWait=new WebDriverWait(driver, Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert =driver.switchTo().alert();
        String  text =alert.getText();
        alert.accept();
        //跳转到主页
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));
        //查看是否更新成功
        driver.get(url);
        WebElement element1 = driver.findElement(By.cssSelector("#title"));
        String title2 = element1.getText();
        assert  ("今天good").equals(title2);
        System.out.println("updateByMySuccess---"+title1+"-->"+text+"-->"+title2);
        try {
            methodName=targetclass.getMethod("updateByMySuccess").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //
    }
    public void updateByMeNull(){
        String methodName="";
        driver.navigate().refresh();
        WebElement element = driver.findElement(By.cssSelector("#title"));
        String title1 = element.getText();
        assert !title1.isEmpty();
        try {
            methodName=targetclass.getMethod("updateByMeNull").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        element.clear();
        System.out.println("updateByMySuccess---" + title1);
        //提交
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait webDriverWait=new WebDriverWait(driver, Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert =driver.switchTo().alert();
        String  text =alert.getText();
        alert.accept();
        //原来页
        driver.navigate().refresh();
        WebElement element2 = driver.findElement(By.cssSelector("#title"));
        String title2 = element2.getText();
        assert title1.equals(title2);
        try {
            methodName=targetclass.getMethod("updateByMeNull").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("updateByMeNull---"+text);
    }

    public void updateByMeOther(){
        String methodName="";
        driver.navigate().refresh();
        driver.navigate().to("http://192.168.47.135:8653/blog_update.html?id=10");
        //不是自己的作品是否可以更新成功
        WebElement element = driver.findElement(By.cssSelector("#title"));
        element.clear();
        element.sendKeys("0806-test");
        driver.findElement(By.cssSelector("#submit")).click();
        WebDriverWait webDriverWait=new WebDriverWait(driver, Duration.ofSeconds(3));
        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert =driver.switchTo().alert();
        String  text =alert.getText();
        alert.accept();
        //页面跳转
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));
        //截屏
        try {
            methodName=targetclass.getMethod("updateByMeOther").getName();
            getScreenShot(targetclass.getName()+"-"+methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("updateByMeOther---"+text);

    }

}